<!--
 * @Author: 卡卡 1071780426@qq.com
 * @Date: 2023-01-06 13:47:30
 * @LastEditors: CNN 969682281@qq.com
 * @LastEditTime: 2023-06-30 14:41:07
 * @FilePath: \mould\src\views\pages\Supervision\deliverability.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <div class="department" id="department">
    <!-- 左侧部门管理 -->
    <div class="structureTree" v-if="treelength">
      <div class="structureTree_title">
        <div>组织机构</div>
        <div>
          <!-- <el-checkbox v-model="checked" @change="see()">查看下级</el-checkbox> -->
        </div>
      </div>
      <tree
        :treeData="treeData"
        :checkData="checkData"
        nodeKey="fCompanyid"
        @getTreeInfo="handleNodeClick"
        :defaultProps="defaultProps"
      >
      </tree>
    </div>
    <!-- 右侧 -->
    <div class="department_right">
      <!-- 机构名称 -->
      <el-breadcrumb :title="showName"></el-breadcrumb>
      <!-- 顶部导航栏 -->
      <div class="mechanism_nav">
        <!-- 左侧 -->
        <div class="mechanism_nav_left" style="width: 100%">
          <vDatePicker
            :defaultDate="value2"
            @selectPickDate="selectTime"
            width="215px !important"
          >
          </vDatePicker>
          <el-select
            class="selectBhz"
            style="width: 160px"
            v-model="getList.stationId"
            placeholder="所有拌和站"
            @change="selectBhz"
          >
            <el-option
              v-for="v in bhzDataList"
              :key="v.fItemName"
              :label="v.fItemName"
              :value="v.fItemValue"
            >
            </el-option>
          </el-select>

          <el-select
            class="selectBhz"
            style="width: 160px"
            v-model="getList.machineId"
            placeholder="所有拌和机"
            @change="yj_searchForm(getList)"
          >
            <el-option
              v-for="v in bhjDataList"
              :key="v.fItemName"
              :label="v.fItemName"
              :value="v.fItemValue"
            >
            </el-option>
          </el-select>
          <!-- <el-select class="selectBhz" style="width: 160px" v-model="getList.machineId" placeholder="所有拌和机"
                                                                                                                                                    @change="yj_searchForm(getList)">
                                                                                                                                                    <el-option v-for="v in bhjDataList" :key="v.fItemName" :label="v.fItemName" :value="v.fItemValue">
                                                                                                                                                    </el-option>
                                                                                                                                                </el-select> -->
        </div>
        <!-- 顶部按钮 -->
        <div class="print"></div>
      </div>

      <!-- 表格 -->
      <div class="text_tables" style="padding-top: 15px">
        <div
          class="tableHead"
          :style="{ width: tableBody.length == 0 ? '100%' : '2rem' }"
        >
          <template v-for="(i, j) in tableHead">
            <el-row :key="j">
              <el-col
                :span="i.children ? 12 : 24"
                :style="i.children ? extent(i.children) : 'height:50px'"
                >{{ i.name }}</el-col
              >
              <el-col v-if="i.children" :span="12">
                <template v-for="(g, k) in i.children">
                  <el-row :key="k">{{ g.name }}</el-row>
                </template>
              </el-col>
            </el-row>
          </template>
        </div>
        <div class="tableBody">
          <template v-for="(i, j) in tableBody">
            <el-row :key="j" class="singleRow">
              <template v-for="(g, k) in i">
                <el-col :span="24" class="column" :key="k">
                  <div style="width: 100%" :class="k" class="tr_table">
                    {{ g }}
                  </div>
                </el-col>
              </template>
            </el-row>
          </template>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import vDatePicker from "@/components/public/v-DatePicker";
import tree from "@/components/public/el-trees"; //左侧机构树
import elBreadcrumb from "@/components/public/el-breadcrumb.vue"; //面包屑
import {
  getUserCompanyTreeList, //左侧机构树接口
  getConcreteExceeding, // 查询-混凝土超标统计
  getStationmachineList, //拌合站列表
  getMachineListByStationId, //拌合机
} from "@/api/request.js";

export default {
  name: "exceedance",
  data() {
    return {
      name: "exceedance", //按钮配置页面代号
      formData: {}, //新增编辑向后台传递对象
      bhjDataList: [], //拌合机
      bhzDataList: [], //拌合站
      value2: "", //主页面日期筛选绑定值
      highlightBd: true, //左侧机构树停留背景色
      treelength: false, //左侧树显示隐藏
      checked: true, //查看下级按钮
      showName: "未选择公司",
      fId: null, //点击当前列表数据fId
      listLoading: false, //列表数据加载loading
      // 列表给后端传参
      getList: {
        companyId: "", // 施工标段id
        startDates: "", // 开始时间
        endDates: "", // 截至时间
        machineId: "", // 拌和机编号
        enCode: "",
        stationId: "",
      },
      companyTree: "", //所有数据
      treeData: [], //左侧树状图数据
      // 左侧机构树
      defaultProps: {
        children: "children",
        label: "fShortname",
      },
      checkData: [],
      userInfo: {}, //登录用户
      treeText: {
        //树状图参数
        companyId: null,
        userId: null,
        text: null,
      },
      xAllData: [],
      tableHead: [
        { name: "强度等级", id: 0 },
        { name: "生产总盘数", id: 1 },
        {
          name: "初级",
          id: 2,
          children: [
            { name: "超标盘数", id: 2 - 0 },
            { name: "超标率", id: 2 - 1 },
          ],
        },
        {
          name: "中",
          id: 3,
          children: [
            { name: "超标盘数", id: 3 - 0 },
            { name: "超标率", id: 3 - 1 },
          ],
        },
        {
          name: "高级",
          id: 4,
          children: [
            { name: "超标盘数", id: 4 - 0 },
            { name: "超标率", id: 4 - 1 },
          ],
        },
        { name: "合计超标率", id: 5 },
      ],
      tableBody: [],
    };
  },
  components: {
    vDatePicker,
    tree, //左侧组织机构数
    // TableData, //主页面列表
    elBreadcrumb, //面包屑

    // myCharts,
  },
  mounted() {
    this.userInfo = JSON.parse(sessionStorage.getItem("yjSmart-userInfo"));
    if (JSON.parse(sessionStorage.getItem("yjSmart-getLists"))) {
      var data = JSON.parse(sessionStorage.getItem("yjSmart-getLists"));
      this.getList.companyId = data.fCompanyid; // 施工标段id
      this.getList.startDates = data.startDates; // 开始时间
      this.getList.endDates = data.endDates; // 截至时间
      this.getList.machineId = data.machineId; // 拌和机编号
      this.getList.stationId = data.stationId;
      this.getbhjDataMethods(this.getList.stationId);
    }
    if (JSON.parse(sessionStorage.getItem("yjSmart-treeCheck"))) {
      var id = JSON.parse(sessionStorage.getItem("yjSmart-treeCheck"));
      this.checkData = [id.fCompanyid];
    }
    this.getTree();
    this.btnRole(this.name);
    this.getDate();
  },

  methods: {
    btnRole(name) {
      // 按钮权限
      var btnList = [];
      var ModuleList = JSON.parse(sessionStorage.getItem("yjSmart-ModuleList"));
      if (ModuleList.length > 0) {
        ModuleList.map((item) => {
          if (item.fEncode == name) {
            btnList = item.modulebuttonList;
          }
        });
        var btnStr = [];
        btnList.map((item) => {
          btnStr.push(item.fEncode);
        });
        // this.yj_view = btnStr.includes("yj_view");
      } else {
        // this.yj_view = true;
      }
    },

    // 左侧树结构
    getTree() {
      this.treeText = {
        companyId: this.userInfo.companyId,
        userId: this.userInfo.userId,
        text: "1", // 0其他  1 砼拌合站 2水稳 3沥青
      };
      getUserCompanyTreeList(this.treeText)
        .then((res) => {
          if (res.errcode == 0) {
            var companyTree = res.data.userCompanyTreeList;
            this.treeData = this.commonJs.tranListToTreeData(companyTree);

            if (this.treeData[0].children == undefined) {
              this.treelength = false; //左侧组织机构是否显示
            } else {
              this.treelength = true;
            }

            if (this.checkData[0]) {
              this.showName = JSON.parse(
                sessionStorage.getItem("yjSmart-treeCheck")
              ).fShortname;

              this.getList.companyId = this.checkData[0];
            } else {
              this.showName = this.treeData[0].fShortname; //当前点击组织机构名称
              this.getList.companyId = this.treeData[0].fCompanyid; //当前点击组织机构ID
              this.checkData[0] = this.treeData[0].fCompanyid;
            }
            this.mixingPlantList(0, this.getList);

            this.yj_searchForm(this.getList); // 调用主页面列表接口
            // 列表
          } else {
            this.$message.error(res.msg);
            // this.$message.error("获取组织机构失败");
            return false;
          }
        })
        .catch(() => {
          return false;
        });
    },

    // 点击左侧机构树
    // handleNodeClick(data) {
    //     this.showName = data.fShortname; //顶部显示机构名
    //     this.getList.companyId = data.fCompanyid;
    //     this.mixingPlantList(0, this.getList);
    //     this.getList.startDates = this.common.getDay(-6) + " " + '00:00:00'// 开始时间
    //     this.getList.endDates = this.common.getDay(0) + " " + "23:59:59"// 截至时间
    //     this.getList.machineId = ""; // 拌和机编号
    //     this.getList.stationId = ""

    //     this.value2 = [this.common.getDay(-6) + " " + '00:00:00', this.common.getDay(0) + " " + "23:59:59"]
    //     this.yj_searchForm(this.getList);
    // },
    handleNodeClick(data) {
      this.showName = data.fShortname; //顶部显示机构名
      if (JSON.parse(sessionStorage.getItem("yjSmart-getLists"))) {
        var datas = JSON.parse(sessionStorage.getItem("yjSmart-getLists"));
        this.getList.companyId = data.fCompanyid;
        this.getList.startDates = datas.startDates; // 开始时间
        this.getList.endDates = datas.endDates; // 截至时间
        this.value2 = [this.getList.startDates, this.getList.endDates];
      } else {
        this.getList.companyId = data.fCompanyid;
        this.getList.startDates = this.common.getDay(-6) + " " + "00:00:00"; // 开始时间
        this.getList.endDates = this.common.getDay(0) + " " + "23:59:59"; // 截至时间
        this.value2 = [
          this.common.getDay(-6) + " " + "00:00:00",
          this.common.getDay(0) + " " + "23:59:59",
        ];
      }
      this.getList.machineId = ""; // 拌和机编号
      this.getList.stationId = "";
      this.mixingPlantList(0, this.getList);
      this.yj_searchForm(this.getList);
    },
    // 获取列表数据
    yj_searchForm(data) {
      this.listLoading = true;
      window.sessionStorage.setItem("yjSmart-getLists", JSON.stringify(data));
      var sc = {
        companyId: data.companyId, // 施工标段id
        startDates: data.startDates, // 开始时间
        endDates: data.endDates, // 截至时间
        machineId: data.machineId, // 拌和机编号
        enCode: "",
        stationId: data.stationId,
      };

      getConcreteExceeding(sc)
        .then((res) => {
          if (res.errcode == 0) {
            var that = this;
            that.listLoading = false;
            var data = res.data.concreteExceeding;

            if (data.length == 1) {
              this.tableBody = [];
            } else {
              // this.tableBody = data
              var fristData = [];
              var DiskNumber = []; //盘数
              var warn1 = []; //初级盘数
              var warn1Price = []; //初级超标率
              var warn2 = []; //中级盘数
              var warn2Price = []; //中级超标率
              var warn3 = []; //高级盘数
              var warn3Price = []; //高级超标率
              var warnSumPrice = []; //合计
              data.map((item) => {
                fristData.push({
                  StrengthGrade: item.StrengthGrade, //强度

                  DiskNumber: item.DiskNumber, //盘数
                  warn1: item.warn1, //初级盘数
                  warn1Price: item.warn1Price + "%", //初级超标率
                  warn2: item.warn2, //中级盘数
                  warn2Price: item.warn2Price + "%", //中级超标率
                  warn3: item.warn3, //高级盘数
                  warn3Price: item.warn3Price + "%", //高级超标率
                  warnSumPrice: item.warnSumPrice + "%", //合计
                });
                DiskNumber.push(item.DiskNumber); //盘数
                warn1.push(item.warn1); //初级盘数
                warn1Price.push(item.warn1Price); //初级超标率
                warn2.push(item.warn2); //中级盘数
                warn2Price.push(item.warn2Price); //中级超标率
                warn3.push(item.warn3); //高级盘数
                warn3Price.push(item.warn3Price); //高级超标率
                warnSumPrice.push(item.warnSumPrice); //合计
              });

              DiskNumber.map((item, index) => {
                fristData[fristData.length - 1].DiskNumber += item; //盘数
                fristData[fristData.length - 1].warn1 += warn1[index]; //初级盘数
                fristData[fristData.length - 1].warn2 += warn2[index]; //中级级盘数
                fristData[fristData.length - 1].warn3 += warn3[index]; //高级盘数
              });
              // 初级超标率
              if (fristData[fristData.length - 1].warn1 != 0) {
                fristData[fristData.length - 1].warn1Price =
                  (
                    (fristData[fristData.length - 1].warn1 /
                      fristData[fristData.length - 1].DiskNumber) *
                    100
                  ).toFixed(2) -
                  0 +
                  "%";
              } else {
                fristData[fristData.length - 1].warn1Price = 0;
              }
              //中级超标率
              if (fristData[fristData.length - 1].warn2 != 0) {
                fristData[fristData.length - 1].warn2Price =
                  (
                    (fristData[fristData.length - 1].warn2 /
                      fristData[fristData.length - 1].DiskNumber) *
                    100
                  ).toFixed(2) -
                  0 +
                  "%";
              } else {
                fristData[fristData.length - 1].warn2Price = 0;
              }

              if (fristData[fristData.length - 1].warn3 != 0) {
                fristData[fristData.length - 1].warn3Price =
                  (
                    (fristData[fristData.length - 1].warn3 /
                      fristData[fristData.length - 1].DiskNumber) *
                    100
                  ).toFixed(2) -
                  0 +
                  "%";
              } else {
                fristData[fristData.length - 1].warn3Price = 0 + "%";
              }

              fristData[fristData.length - 1].warnSumPrice =
                (
                  parseFloat(fristData[fristData.length - 1].warn1Price) +
                  parseFloat(fristData[fristData.length - 1].warn2Price) +
                  parseFloat(fristData[fristData.length - 1].warn3Price)
                ).toFixed(2) + "%";

              // (fristData[fristData.length - 1].warn1Price +
              // fristData[fristData.length - 1].warn2Price +
              // fristData[fristData.length - 1].warn3Price).toFixed(2) - 0

              this.tableBody = fristData;
            }
          } else {
            this.$message.error(res.msg);
            return false;
          }
        })
        .catch((error) => {
          console.log(error);
        });
    },

    // 获取拌合站数据
    mixingPlantList(num, datas) {
      // // this.getList.fCompanyid this.checkData[0]
      var data = {
        companyId: datas.companyId, // 施工标段id
        modelType: num, // 类型
        stationType: "1", // 拌合站类型
      };

      // this.bhzDataList = [];
      getStationmachineList(data)
        .then((res) => {
          if (res.errcode == 0) {
            var arr = res.data.stationmachineList;

            // if (num == 1) {
            //   this.bhjDataList = [
            //     {
            //       fItemName: "所有拌和机",
            //       fItemValue: "",
            //     },
            //   ];
            //   arr.map((item) => {
            //     this.bhjDataList.push({
            //       fItemName: item.fMachinename,
            //       fItemValue: item.fMachineid
            //     });
            //   });
            // } else
            // if (num == 0) {
            this.bhzDataList = [
              {
                fItemName: "所有拌和站",
                fItemValue: "",
              },
            ];

            arr.map((item) => {
              this.bhzDataList.push({
                fItemName: item.fMachinename,
                fItemValue: item.fMachineid,
              });
            });
            console.log(8888, this.bhzDataList);
            // }
          } else {
            this.$message.error(res.msg);
            return false;
          }
        })
        .catch((error) => {
          console.log(error);
        });
    },
    // 拌合站选择
    selectBhz(e) {
      console.log(e);
      this.getList.machineId = "";
      this.getbhjDataMethods(e);
      // this.yj_searchForm(this.getList)
    },

    getbhjDataMethods(data) {
      getMachineListByStationId({ stationId: data })
        .then((res) => {
          if (res.errcode == 0) {
            var arr = res.data.stationmachineList;

            this.bhjDataList = [
              {
                fItemName: "所有拌和机",
                fItemValue: "",
              },
            ];
            arr.map((item) => {
              this.bhjDataList.push({
                fItemName: item.fMachinename,
                fItemValue: item.fMachineid,
              });
            });
          } else {
            this.$message.error(res.msg);
            return false;
          }
        })
        .catch((error) => {
          console.log(error);
        });
    },
    extent(list) {
      var height = 50 * list.length;
      return `height: ${height}px; line-height: ${height}px;`;
    },
    // 时间间隔
    // selectTime(e) {
    //     this.getList.startDates = e[0] + " " + "00:00:00"; // 开始时间
    //     this.getList.endDates = e[1] + " " + "23:59:59"; // 截至时间
    //     this.xlsxName =
    //         e[0] + "至" + e[1] + "混凝土拌和站记录数据";

    //     this.yj_searchForm(this.getList);
    // },
    selectTime(e) {
      if (e[0] != "" && e[1] != "") {
        this.getList.startDates = e[0] + " " + "00:00:00"; // 开始时间
        this.getList.endDates = e[1] + " " + "23:59:59"; // 截至时间
      } else {
        this.getList.startDates = ""; // 开始时间
        this.getList.endDates = ""; // 截至时间
      }
      this.yj_searchForm(this.getList);
    },

    getDate() {
      if (JSON.parse(sessionStorage.getItem("yjSmart-getLists"))) {
        var data = JSON.parse(sessionStorage.getItem("yjSmart-getLists"));
        this.getList.startDates = data.startDates; // 开始时间
        this.getList.endDates = data.endDates; // 截至时间
        this.getList.machineId = data.machineId; // 拌和机编号
        this.value2 = [this.getList.startDates, this.getList.endDates];
        this.xlsxName =
          this.getList.startDates +
          "至" +
          this.getList.endDates +
          "混凝土拌和站记录数据";
      } else {
        this.getList.startDates = this.common.getDay(-6) + " " + "00:00:00"; // 开始时间
        this.getList.endDates = this.common.getDay(0) + " " + "23:59:59"; // 截至时间
        this.value2 = [
          this.common.getDay(-6) + " " + "00:00:00",
          this.common.getDay(0) + " " + "23:59:59",
        ];
        this.xlsxName =
          this.common.getDay(-6) +
          "至" +
          this.common.getDay(0) +
          " " +
          "23:59:59" +
          "混凝土拌和站记录数据";
      }
    },
    // 多选方法
    selectionChange() {},
    // 排序方法
    sortChange() {},
  },
};
</script>

<style scoped>
.text_tables {
  width: 99.5%;

  display: flex;
}

.tableHead {
  /* width: 2rem; */
  float: left;
  /* background: #edeaea; */
  background: #f5f5f6;
  color: #4f6375;
  font-weight: 700;
}

.StrengthGrade {
  /* background: #edeaea; */
  background: #f5f5f6;
  color: #4f6375;
  font-weight: 700;
}

.tableBody {
  flex: 1;
  display: flex;
  float: left;
  border-right: 1px solid #e6e6e6;
}

.singleRow {
  flex: 1;
}

.el-row {
  margin: -1px -1px -1px -1px;
  /*解决边框重叠*/
  text-align: center;
  font-size: 14px;
  line-height: 50px;
  border: 1px solid #e6e6e6;
  border-right: none;
}

.tr_table {
  border: none;
  border-left: 1px solid #e6e6e6;
}

.column {
  border-top: 1px solid #e6e6e6;
  border-bottom: 1px solid #e6e6e6;

  height: 51px;
  border-top: none;
  border-left: none;
}
</style>
